package com.imooc.service;

import com.imooc.enums.ResultEnum;
import com.imooc.exception.StudentException;
import com.imooc.repository.StuRepository;
import com.imooc.domain.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class StuService {
    @Autowired
    private StuRepository stuRepository;
    //TODO:回滚无效
    @Transactional
    public void insertTwo() {
        Student stu1 = new Student();
        stu1.setAge(19);
        stu1.setName("E");
        stu1.setScore(177.4);
        stuRepository.save(stu1);

        Student stu2 = new Student();
        stu2.setAge(22);
        stu2.setName("Mikekkkkkk");
        stu2.setScore(122.4);
        stuRepository.save(stu2);

    }

    public  void getAge(Integer id) throws Exception{
        Student student = stuRepository.findById(id).get();
        Integer age = student.getAge();
        if(age<10){
            //返回“小学生”
            throw new StudentException(ResultEnum.PRIMARY_SHCOOL);
        }else if(age>=10 && age<=16){
            //返回“初中生”
            throw new StudentException(ResultEnum.MIDDLE_SHCOOL);
        }
    }
}
